<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="APPID" prop="appId">
          <el-input v-model="dataForm.appId" placeholder="小程序_公众号_H5(APPID)"></el-input>
      </el-form-item>
      <el-form-item label="JSAPI APPID" prop="appAppidCode">
          <el-input v-model="dataForm.appAppidCode" placeholder=" 微信小程序/微信公众号APPID(JSAPI支付专属)  参数配置对应"></el-input>
      </el-form-item>
      <el-form-item label="JSAPI SECRET" prop="appSecretCode">
          <el-input v-model="dataForm.appSecretCode" placeholder="微信小程序/微信公众号密钥(JSAPI支付专属)  参数配置对应"></el-input>
      </el-form-item>
      <el-form-item label="商户号" prop="merchantId">
          <el-input v-model="dataForm.merchantId" placeholder="商户号"></el-input>
      </el-form-item>
          <el-form-item label="密钥" prop="appSecret">
          <el-input v-model="dataForm.appSecret" placeholder="密钥"></el-input>
      </el-form-item>
          <el-form-item label="私钥" prop="privateKey">
          <el-input v-model="dataForm.privateKey" placeholder="私钥"></el-input>
      </el-form-item>
          <el-form-item label="公钥" prop="publicKey">
          <el-input v-model="dataForm.publicKey" placeholder="公钥"></el-input>
      </el-form-item>
          <el-form-item label="支付URL" prop="payUrl">
          <el-input v-model="dataForm.payUrl" placeholder="支付网关URL(支付请求URL地址)"></el-input>
      </el-form-item>
          <el-form-item label="URL拼接" prop="payUrlMontage">
          <el-input v-model="dataForm.payUrlMontage" placeholder="支付网关URL拼接字段(该字段允许为NULL 应对某些支付需要单独拼接URL字符串)"></el-input>
      </el-form-item>
          <el-form-item label="回调地址" prop="notifyUrl">
          <el-input v-model="dataForm.notifyUrl" placeholder="支付回调地址"></el-input>
      </el-form-item>
      <el-form-item label="跳转地址" prop="returnUrl">
          <el-input v-model="dataForm.returnUrl" placeholder="支付成功或者失败后跳转地址"></el-input>
      </el-form-item>
          <el-form-item label="商户名称" prop="merchantsName">
          <el-input v-model="dataForm.merchantsName" placeholder="商户名称(例:摸摸鱼科技)"></el-input>
      </el-form-item>
          <el-form-item label="支付名称" prop="payName">
          <el-input v-model="dataForm.payName" placeholder="支付名称(用于前端页面显示 例:支付宝H5,微信官方,汇付小程序等)"></el-input>
      </el-form-item>
      <el-form-item prop="type" :label="$t('payconfig.type')" size="mini">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="1">{{ $t('payconfig.type1') }}</el-radio>
          <el-radio :label="2">{{ $t('payconfig.type2') }}</el-radio>
          <el-radio :label="3">{{ $t('payconfig.type3') }}</el-radio>
          <el-radio :label="4">{{ $t('payconfig.type4') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否开启" prop="status" :label="$t('commodity.status')" size="mini">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{ $t('commodity.status1') }}</el-radio>
          <el-radio :label="2">{{ $t('commodity.status2') }}</el-radio>
        </el-radio-group>
      </el-form-item>
          <el-form-item label="通道备注" prop="remark">
          <el-input v-model="dataForm.remark" placeholder="通道备注"></el-input>
      </el-form-item>
          <el-form-item label="调用名称" prop="callName">
          <el-input v-model="dataForm.callName" placeholder="通道调用名称(该字段请严格按照代码接口填写 否则无法调用支付接口)"></el-input>
      </el-form-item>
          <el-form-item label="通道图标" prop="icon">
          <el-input v-model="dataForm.icon" placeholder="支付通道图标(用于前端页面显示)"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        appId: '',
        appSecret: '',
        privateKey: '',
        publicKey: '',
        payUrl: '',
        payUrlMontage: '',
        notifyUrl: '',
        merchantsName: '',
        payName: '',
        type: '',
        status: '',
        amountSuccess: '',
        remark: '',
        callName: '',
        icon: '',
        time: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        notifyUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        payName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        status: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        amountSuccess: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        callName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        icon: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        time: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/payconfig/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/payconfig/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
